import os
import sys
from django.core.management import setup_environ
import subprocess
import time
from psn_django import *
from datetime import datetime
from django.db import connection, transaction

'''
	 This module is used to fork the current process into a daemon.
	 Almost none of this is necessary (or advisable) if your daemon
	 is being started by inetd. In that case, stdin, stdout and stderr are
	 all set up for you to refer to the network connection, and the fork()s
	 and session manipulation should not be done (to avoid confusing inetd).
	 Only the chdir() and umask() steps remain as useful.
	 References:
		  UNIX Programming FAQ
				1.7 How do I get my program to act like a daemon?
					 http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
		  Advanced Programming in the Unix Environment
				W. Richard Stevens, 1992, Addison-Wesley, ISBN 0-201-56317-7.

	 History:
		2002/08/28 by Noah Spurrier
		2003/02/24 by Clark Evans

		http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
'''
import sys, os, time
from signal import SIGTERM

def deamonize(stdout='/dev/null', stderr=None, stdin='/dev/null',
				  pidfile=None, startmsg = 'started with pid %s' ):
	 '''
		  This forks the current process into a daemon.
		  The stdin, stdout, and stderr arguments are file names that
		  will be opened and be used to replace the standard file descriptors
		  in sys.stdin, sys.stdout, and sys.stderr.
		  These arguments are optional and default to /dev/null.
		  Note that stderr is opened unbuffered, so
		  if it shares a file with stdout then interleaved output
		  may not appear in the order that you expect.
	 '''
	 # Do first fork.
	 try:
		  pid = os.fork()
		  if pid > 0: sys.exit(0) # Exit first parent.
	 except OSError, e:
		  sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
		  sys.exit(1)

	 # Decouple from parent environment.
	 os.chdir("/home/fabriciols/ps3t/myps3t")
	 os.umask(0)
	 os.setsid()

	 # Do second fork.
	 try:
		  pid = os.fork()
		  if pid > 0: sys.exit(0) # Exit second parent.
	 except OSError, e:
		  sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
		  sys.exit(1)

	 # Open file descriptors and print start message
	 if not stderr: stderr = stdout
	 si = file(stdin, 'r')
	 so = file(stdout, 'a+')
	 se = file(stderr, 'a+', 0)
	 pid = str(os.getpid())
	 sys.stderr.write("\n%s\n" % startmsg % pid)
	 sys.stderr.flush()
	 if pidfile: file(pidfile,'w+').write("%s\n" % pid)
	 # Redirect standard file descriptors.
	 os.dup2(si.fileno(), sys.stdin.fileno())
	 os.dup2(so.fileno(), sys.stdout.fileno())
	 os.dup2(se.fileno(), sys.stderr.fileno())

def startstop(stdout='/dev/null', stderr=None, stdin='/dev/null',
				  pidfile='new_user.pid', startmsg = 'started with pid %s' ):
	 if len(sys.argv) > 1:
		  action = sys.argv[1]
		  try:
				pf  = file(pidfile,'r')
				pid = int(pf.read().strip())
				pf.close()
		  except IOError:
				pid = None
		  if 'stop' == action or 'restart' == action:
				if not pid:
					 mess = "Could not stop, pid file '%s' missing.\n"
					 sys.stderr.write(mess % pidfile)
					 sys.exit(1)
				try:
					while 1:
						 os.kill(pid,SIGTERM)
						 time.sleep(1)
				except OSError, err:
					err = str(err)
					if err.find("No such process") > 0:
						 os.remove(pidfile)
						 if 'stop' == action:
							  sys.exit(0)
						 action = 'start'
						 pid = None
					else:
						 print str(err)
						 sys.exit(1)
		  if 'start' == action:
				if pid:
					 mess = "Start aborded since pid file '%s' exists.\n"
					 sys.stderr.write(mess % pidfile)
					 sys.exit(1)
				deamonize(stdout,stderr,stdin,pidfile,startmsg)
				return
	 print "usage: %s start|stop|restart" % sys.argv[0]
	 sys.exit(2)

if __name__ == "__main__":
	file_out = '/home/fabriciols/ps3t/myps3t/new_user.txt'

	startstop(stdout=file_out, stderr=file_out)

	while 1:
		# Verifica se algum novo usuario foi adicionado a base de dados

		newUsers = db.updateQueue.objects.filter(type='1')

		if len(newUsers) is not 0:
			for user in newUsers:
				sys.stdout.write ('%s :: |%s| |%s| |%s| Start\n' %(datetime.now(),user.user.username ,user.type ,user.date))
				sys.stdout.flush()
				subprocess.call(['python', '/home/fabriciols/ps3t/myps3t/psn.py', 'MAJOR', '%s' %user.user.username])
				user.type = '2'
				user.save()
				sys.stdout.write ('%s :: |%s| |%s| |%s| End\n' %(datetime.now(),user.user.username ,user.type ,user.date))

				query = 'insert into smf_members (memberName, realName, emailAddress, passwd, dateRegistered, avatar, birthdate) (select b.username,b.username,b.email,b.password,UNIX_TIMESTAMP(b.date_joined),a.pic_url,a.birth from myps3t_userinfo a, auth_user b where a.auth_id = b.id and b.id = %d)' %user.user.id

				try:
					cursor = connection.cursor()
					cursor.execute(query)
					transaction.commit_unless_managed()
				except:
					pass

		else:
			pass
			#sys.stdout.write ('%s :: No new user\n' %(datetime.now()))
			#sys.stdout.flush()

		time.sleep(10)
